﻿using Bogus;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using TodoList.Shared;

namespace TodoList.Data
{
	public static class FakeDataSeedingExtension
	{
		public static void MakeSureDbCreated(this WebApplication app)
		{
			using var scope = app.Services.CreateScope();
			var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
            
			var todoFaker = new Faker<TodoItem>()
				.RuleFor(o => o.Title, f => f.Lorem.Sentence())
				.RuleFor(o => o.Description, f => f.Lorem.Paragraph())
				.RuleFor(o => o.IsCompleted, f => f.Random.Bool())
				.RuleFor(o => o.DueDate, f => f.Date.Future());

			var todoItems = todoFaker.Generate(10);
			db.TodoItems.AddRange(todoItems);
			db.SaveChanges();
		}
	}
}
